class Solution {
public:
    long long pickGifts(vector<int>& gifts, int k) {
        priority_queue<int> q(gifts.begin(), gifts.end());
        long long count = 0;

        while(k-- != 0)
        {
            int x = q.top();
            if(x == 1) break;
            int y = sqrt(x);
            q.pop();
            q.push(y);
        }

        while(!q.empty())
        {
            count += q.top();
            q.pop();
        }
        
        return count;
    }

};
